import axios from 'axios'
import { ref } from 'vue'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import { ListingInfoControllerService } from '../../generated'

const user = useUserStore()
/**
 * 充值碳币
 * @param account 地址
 * @param balances 数量
 */
export const addBalance = async (account: any, balances: any) => {
  const data = {
    cnsName: '',
    contractAbi: [
      {
        constant: false,
        funcId: 10,
        inputs: [
          {
            name: 'to',
            type: 'address',
            value: account
          },
          {
            name: 'amount',
            type: 'uint256',
            value: balances
          }
        ],
        name: 'transfer',
        outputs: [
          {
            name: '',
            type: 'bool'
          }
        ],
        payable: false,
        stateMutability: 'nonpayable',
        type: 'function'
      }
    ],
    contractAddress: '0x7b577ff4e056113765022c32a7157bffcb5de7b3',
    contractId: 200003,
    contractName: 'MyToken',
    funcName: 'transfer',
    funcParam: [account, balances],
    groupId: '1',
    useCns: false,
    user: '0x0876bba2fec3cba7c70be10406dd84b480dead31',
    version: ''
  }
  const ans = ref({
    status: 0
  })
  await axios
    .post('/api/WeBASE-Front/trans/handle', data)
    .then(() => {})
    .catch(() => {
      ans.value.status = 1
    })

  return ans.value
}

/**
 * 获取余额
 * @param account
 */
export const getBalance = async (account: any) => {
  const data = ref({
    cnsName: '',
    contractAbi: [
      {
        constant: true,
        funcId: 7,
        inputs: [
          {
            name: 'account',
            type: 'address',
            value: account
          }
        ],
        name: 'balanceOf',
        outputs: [
          {
            name: '',
            type: 'uint256'
          }
        ],
        payable: false,
        stateMutability: 'view',
        type: 'function'
      }
    ],
    contractAddress: '0x7b577ff4e056113765022c32a7157bffcb5de7b3',
    contractId: 200003,
    contractName: 'MyToken',
    funcName: 'balanceOf',
    funcParam: [account],
    groupId: '1',
    useCns: false,
    user: '',
    version: ''
  })
  const ans = ref<string>('0')
  await axios
    .post('/api/WeBASE-Front/trans/handle', data.value)
    .then((res) => {
      ans.value = res.data[0]
    })
    .catch((res) => {
      ans.value = res.data[0]
    })

  return ans.value
}

/**
 * 获取用户可交易碳配额
 * @param account
 */
export const getTransationEmission = async (account: any) => {
  const data = {
    cnsName: '',
    contractAbi: [
      {
        constant: true,
        funcId: 6,
        inputs: [
          {
            name: '_t',
            type: 'address',
            value: account
          }
        ],
        name: 'getTransationEmission',
        outputs: [
          {
            name: '',
            type: 'int256'
          }
        ],
        payable: false,
        stateMutability: 'view',
        type: 'function'
      }
    ],
    contractAddress: '0xa3a018dd6776680a13c89cebea2b88e44770f0ed',
    contractId: 200006,
    contractName: 'Emission',
    funcName: 'getTransationEmission',
    funcParam: [account],
    groupId: '1',
    useCns: false,
    user: '',
    version: ''
  }
  const ans = ref({
    status: 0,
    transationEmission: 0
  })
  await axios
    .post('/api/WeBASE-Front/trans/handle', data)
    .then((res) => {
      ans.value.transationEmission = res.data[0]
      user.setTransationEmission(res.data[0])
    })
    .catch(() => {
      ans.value.status = 1
    })

  return ans.value
}

/**
 * 碳币交易
 * @param from  卖方地址
 * @param to 买方地址
 * @param amount 交易数量
 */
export const transferCarbonCoin = async (from: any, to: any, amount: any) => {
  const data = {
    cnsName: '',
    contractAbi: [
      {
        constant: false,
        funcId: 5,
        inputs: [
          {
            name: 'from',
            type: 'address',
            value: from
          },
          {
            name: 'to',
            type: 'address',
            value: to
          },
          {
            name: 'amount',
            type: 'uint256',
            value: amount
          }
        ],
        name: 'transferFrom',
        outputs: [
          {
            name: '',
            type: 'bool'
          }
        ],
        payable: false,
        stateMutability: 'nonpayable',
        type: 'function'
      }
    ],
    contractAddress: '0x7b577ff4e056113765022c32a7157bffcb5de7b3',
    contractId: 200003,
    contractName: 'MyToken',
    funcName: 'transferFrom',
    funcParam: [from, to, amount],
    groupId: '1',
    useCns: false,
    user: '0x0876bba2fec3cba7c70be10406dd84b480dead31',
    version: ''
  }
  const ans = ref({
    blockchainId: '',
    status: 0
  })
  await axios
    .post('/api/WeBASE-Front/trans/handle', data)
    .then(async (res) => {
      ans.value.blockchainId = res.data.transactionHash
    })
    .catch(() => {
      ans.value.status = 1
    })

  return ans.value
}

/**
 * 碳配额交易  把暂存在中间人的碳配额转移到买方
 * @param from  卖方地址
 * @param to 买方地址
 * @param amount 碳配额转移数量
 */
export const quotaShare = async (from: any, to: any, amount: any) => {
  const data = ref({
    cnsName: '',
    contractAbi: [
      {
        constant: false,
        funcId: 7,
        inputs: [
          {
            name: 'form',
            type: 'address',
            value: from
          },
          {
            name: 'to',
            type: 'address',
            value: to
          },
          {
            name: 'amount',
            type: 'int256',
            value: amount
          }
        ],
        name: 'transation',
        outputs: [],
        payable: false,
        stateMutability: 'nonpayable',
        type: 'function'
      }
    ],
    contractAddress: '0xa3a018dd6776680a13c89cebea2b88e44770f0ed',
    contractId: 200006,
    contractName: 'Emission',
    funcName: 'transation',
    funcParam: [from, to, amount],
    groupId: '1',
    useCns: false,
    user: '0x0876bba2fec3cba7c70be10406dd84b480dead31',
    version: ''
  })
  const ans = ref({
    status: 0
  })
  await axios
    .post('/api/WeBASE-Front/trans/handle', data.value)
    .then(async () => {})
    .catch(() => {
      ans.value.status = 1
    })

  return ans.value
}

/**
 * 把可交易的碳配额转移到中间人
 * @param to 卖方地址
 * @param amount 碳配额转移数量
 */
export const transferToIntermediaries = async (to: any, amount: any) => {
  const data = {
    cnsName: '',
    contractAbi: [
      {
        constant: false,
        funcId: 7,
        inputs: [
          {
            name: 'to',
            type: 'address',
            value: to
          },
          {
            name: 'amount',
            type: 'int256',
            value: amount
          }
        ],
        name: 'approveTransatoin',
        outputs: [
          {
            name: '',
            type: 'bool'
          }
        ],
        payable: false,
        stateMutability: 'nonpayable',
        type: 'function'
      }
    ],
    contractAddress: '0xa3a018dd6776680a13c89cebea2b88e44770f0ed',
    contractId: 200006,
    contractName: 'Emission',
    funcName: 'approveTransatoin',
    funcParam: [to, amount],
    groupId: '1',
    useCns: false,
    user: '0x0876bba2fec3cba7c70be10406dd84b480dead31',
    version: ''
  }
  const ans = ref({
    status: 0
  })
  await axios
    .post('/api/WeBASE-Front/trans/handle', data)
    .then(async () => {})
    .catch(() => {
      ans.value.status = 1
    })

  return ans.value
}

/**
 * //上传碳排放数据
 * @param to
 * @param when
 * @param amount
 */
export const setCarbonEmission = async (to: any, when: any, amount: any) => {
  const data = {
    cnsName: '',
    contractAbi: [
      {
        constant: false,
        funcId: 5,
        inputs: [
          {
            name: 'to',
            type: 'address',
            value: to
          },
          {
            name: 'when',
            type: 'string',
            value: when
          },
          {
            name: 'amount',
            type: 'int256',
            value: amount
          }
        ],
        name: 'addCarbonEmission',
        outputs: [],
        payable: false,
        stateMutability: 'nonpayable',
        type: 'function'
      }
    ],
    contractAddress: '0xa3a018dd6776680a13c89cebea2b88e44770f0ed',
    contractId: 200006,
    contractName: 'Emission',
    funcName: 'addCarbonEmission',
    funcParam: [to, when, amount],
    groupId: '1',
    useCns: false,
    user: '0x0876bba2fec3cba7c70be10406dd84b480dead31',
    version: ''
  }
  const ans = ref({
    status: 0,
    blockchainId: ''
  })
  await axios
    .post('/api/WeBASE-Front/trans/handle', data)
    .then(async (res) => {
      ans.value.blockchainId = res.data.transactionHash
    })
    .catch(() => {
      ans.value.status = 1
    })

  return ans.value
}

/**
 * //查看碳排放数据
 * @param to
 * @param when
 */
export const getCarbonEmission = async (to: any, when: any) => {
  const data = {
    cnsName: '',
    contractAbi: [
      {
        constant: true,
        funcId: 11,
        inputs: [
          {
            name: 'to',
            type: 'address',
            value: to
          },
          {
            name: 'when',
            type: 'string',
            value: when
          }
        ],
        name: 'getCarbonEmission',
        outputs: [
          {
            name: '',
            type: 'int256'
          }
        ],
        payable: false,
        stateMutability: 'view',
        type: 'function'
      }
    ],
    contractAddress: '0xa3a018dd6776680a13c89cebea2b88e44770f0ed',
    contractId: 200006,
    contractName: 'Emission',
    funcName: 'getCarbonEmission',
    funcParam: [to, when],
    groupId: '1',
    useCns: false,
    user: '',
    version: ''
  }
  const ans = ref({
    status: 0,
    carbonEmission: 0
  })
  await axios
    .post('/api/WeBASE-Front/trans/handle', data)
    .then((res) => {
      ans.value.carbonEmission = res.data[0]
    })
    .catch(() => {
      ans.value.status = 1
    })

  return ans.value
}

/**
 * 设置不同类型企业的碳配额
 * @param type
 * @param amount
 */
export const setEmission = async (type: any, amount: any) => {
  const data = {
    cnsName: '',
    contractAbi: [
      {
        constant: false,
        funcId: 8,
        inputs: [
          {
            name: '_t',
            type: 'string',
            value: type
          },
          {
            name: 'amount',
            type: 'int256',
            value: amount
          }
        ],
        name: 'addEmission',
        outputs: [],
        payable: false,
        stateMutability: 'nonpayable',
        type: 'function'
      }
    ],
    contractAddress: '0xa3a018dd6776680a13c89cebea2b88e44770f0ed',
    contractId: 200006,
    contractName: 'Emission',
    funcName: 'addEmission',
    funcParam: [type, amount],
    groupId: '1',
    useCns: false,
    user: '0x0876bba2fec3cba7c70be10406dd84b480dead31',
    version: ''
  }
  const ans = ref({
    status: 0
  })
  await axios
    .post('/api/WeBASE-Front/trans/handle', data)
    .then(() => {})
    .catch(() => {
      ans.value.status = 1
    })

  return ans.value
}

/**
 * 查看不同类型企业的碳配额
 * @param type
 */
export const getEmission = async (type: any) => {
  const data = {
    cnsName: '',
    contractAbi: [
      {
        constant: true,
        funcId: 1,
        inputs: [
          {
            name: '_t',
            type: 'string',
            value: type
          }
        ],
        name: 'emission',
        outputs: [
          {
            name: '',
            type: 'int256'
          }
        ],
        payable: false,
        stateMutability: 'view',
        type: 'function'
      }
    ],
    contractAddress: '0xa3a018dd6776680a13c89cebea2b88e44770f0ed',
    contractId: 200006,
    contractName: 'Emission',
    funcName: 'emission',
    funcParam: [type],
    groupId: '1',
    useCns: false,
    user: '',
    version: ''
  }
  const ans = ref({
    status: 0,
    emission: 0
  })
  await axios
    .post('/api/WeBASE-Front/trans/handle', data)
    .then((res) => {
      ans.value.emission = res.data[0]
    })
    .catch(() => {
      ans.value.status = 1
    })

  return ans.value
}

/**
 * 分发碳配额
 * @param to
 * @param type
 */
export const transferEmission = async (to: any, type: any) => {
  const data = {
    cnsName: '',
    contractAbi: [
      {
        constant: false,
        funcId: 0,
        inputs: [
          {
            name: 'to',
            type: 'address',
            value: to
          },
          {
            name: '_t',
            type: 'string',
            value: type
          }
        ],
        name: 'transfer',
        outputs: [],
        payable: false,
        stateMutability: 'nonpayable',
        type: 'function'
      }
    ],
    contractAddress: '0xa3a018dd6776680a13c89cebea2b88e44770f0ed',
    contractId: 200006,
    contractName: 'Emission',
    funcName: 'transfer',
    funcParam: [to, type],
    groupId: '1',
    useCns: false,
    user: '0x0876bba2fec3cba7c70be10406dd84b480dead31',
    version: ''
  }
  const ans = ref({
    status: 0
  })
  await axios
    .post('/api/WeBASE-Front/trans/handle', data)
    .then(() => {})
    .catch(() => {
      ans.value.status = 1
    })

  return ans.value
}

/**
 * 获取企业的碳配额
 * @param to
 */
export const getCompanyEmission = async (to: any) => {
  const data = {
    cnsName: '',
    contractAbi: [
      {
        constant: true,
        funcId: 4,
        inputs: [
          {
            name: '_address',
            type: 'address',
            value: to
          }
        ],
        name: 'companyEmission',
        outputs: [
          {
            name: '',
            type: 'int256'
          }
        ],
        payable: false,
        stateMutability: 'view',
        type: 'function'
      }
    ],
    contractAddress: '0xa3a018dd6776680a13c89cebea2b88e44770f0ed',
    contractId: 200006,
    contractName: 'Emission',
    funcName: 'companyEmission',
    funcParam: [to],
    groupId: '1',
    useCns: false,
    user: '',
    version: ''
  }
  const ans = ref({
    status: 0,
    emission: 0
  })
  await axios
    .post('/api/WeBASE-Front/trans/handle', data)
    .then(async (res) => {
      ans.value.emission = res.data[0]
    })
    .catch(() => {
      ans.value.status = 1
    })

  return ans.value
}
